from langchain_core.messages import HumanMessage
from custom_llm_zhipu import GLM45V

def create_image_analyzer(api_key):
    """创建图片分析器"""
    llm = GLM45V(api_key=api_key)

    def analyze_image(image_path, prompt_text="请分析这张图片的内容"):
        """分析图片内容"""
        message = HumanMessage(content=[
            {
                "type": "text",
                "text": prompt_text
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": image_path
                }
            }
        ])

        result = llm.generate([[message]])
        return result.generations[0][0].text

    return analyze_image

if __name__ == "__main__":
    analyzer = create_image_analyzer("XXXXXXX")

    image_path = "https://pics0.baidu.com/feed/342ac65c103853430cafd074ea8f206ecb8088b4.jpeg@f_auto?token=ab564cab31e6f058f0648a17f06865af"

    result = analyzer(image_path)
    print("图片分析结果:", result)

    detailed_result = analyzer(
        image_path,
        "What are the pics talk about?"
    )
    print("详细分析:", detailed_result)
